import { observable } from 'mobx';

import service from '@src/service/index';


const showStore = observable({
	title: 'this is home page',
	list: [],
	rightList: [],
	rightWordImgListMap: {},

	// 获取左侧字列表
	async GetWordListById(directoryId) {

		var list = await service.GetWordListById(directoryId)

		this.SetRightListDirect(list);

	},

	SetRightListDirect(list) {
		list.forEach(Item => {
			Item['ImgList'] = [];
			Item['TemplateList'] = [];
			Item['TemplateMap'] = {};
		});
		this.rightList = list;
		window.rightList = this.rightList;

		if (list.length > 0) {
			this.currentWordItem = list[0];
		}
	},


	// 获取左侧字单字
	async GetSingleWordListById(directoryId) {

		//如果已经取过就不再取了
		if (this.rightWordImgListMap[directoryId] == undefined) {
			var list = await service.GetSingleWordListById(directoryId);
			this.rightWordImgListMap[directoryId] = list;
			this.currentWordImgList = this.rightWordImgListMap[directoryId];
		}

		console.log(this.rightWordImgListMap, "this.rightWordImgListMap");

	},


	SetCurrentWordItem(item) {
		this.currentWordItem = item;

		var directoryId = item.DirectoryID

		//如果已存在  就直接赋值
		if (this.rightWordImgListMap[directoryId]) {
			this.currentWordImgList = this.rightWordImgListMap[directoryId];
		} else {
			this.GetSingleWordListById(directoryId);
		}

	},

	GetWordItemById(directoryId) {
		var item = this.rightList.find((item) => { return item.DirectoryID = directoryId; })
		console.log(item, "打到字对象");
		return item;
	},

	currentWordItem: null,
	currentWordImgList: null
});

export default showStore;
